var Lesson4Choose=[]//全局变量 存储该学生的可选课程
$(function () {
getStuLessonsToChoose(1,1,getStuLessonTochoose_desktopHandle);//获取桌面课程
getStuLessonsToChoose(1,3,getStuLessonTochoose_onlineCodeHandle);//获取在线开发课程
getStuLessonsToChoose(1,2,getStuLessonTochoose_onlineMapHandle);//获取在线制图课程
getStuLessonsToChoose(1,4,getStuLessonTochoose_themeMapHandle);//获取专题制图课程
getStuLessonsToChoose(1,99,getStuLessonTochoose_otherHandle);//获取其他课程
});
function  fillRoleAndName(){
	 var s="";
	switch (Role){
		case "admin": s=langThis( "nav_admin" );break;
		case "teacher": s=langThis( "nav_teacher" );break;
		case "student": s=langThis( "nav_student" );break;
		default:break;
	}
	$("#user").text(User);
}
function completer_click(){
	getQuerylessonname($("#querystring").val());
	$("#inputlistcontentdiv").addClass("open");
}
function caculateLesNames(){
if(Lesson4Choose.length<1){
		return [];
	}
	else{
		var  templessons=[];
		for(var i in Lesson4Choose){
			templessons.push(Lesson4Choose[i].lesson_name);
		}

		return templessons;
	}
}
function caculateLesIds(){
if(Lesson4Choose.length<1){
		return [];
	}
	else{
		var  templessons=[];
		for(var i in Lesson4Choose){
			templessons.push(Lesson4Choose[i].lesson_id);
		}

		return templessons;
	}
}
function getQuerylessonname(str) {
    if(Lesson4Choose.length<1){
        // console.log("Lesson4Choose is null")
        return;
    }
	var lessons;
	if(isNaN(str)){
        lessons=caculateLesNames();
    }
    else{
     lessons=caculateLesIds();
    }
	var re = str ? new RegExp(str) : /.*/;
	var lis = "";
	for (var i = 0; i < lessons.length; i++) {
		if (re.test(lessons[i])) {
			lis += '<li data-lessonid="' + lessons[i] + '">' + lessons[i] + '</li>';
		}
	}
	$("ul.dropdown-menu.querymenu").html(lis);
	var q=$("#querystring");
	if (!$("ul.dropdown-menu.querymenu>li").length) {
		$("ul.dropdown-menu.querymenu").append(langThis("kc_nomatches"));
		q.attr("data-lessonid", false);
	}
	if (q.val() === "") {
		q.attr("data-lessonid", false);
	} else {
		q.attr("data-lessonid", $("ul.dropdown-menu.querymenu>li:first-child").attr("data-lessonid"));
	}

	/*绑定查询下拉列表点击事件*/
	$("ul.dropdown-menu.querymenu>li").click(function () {
		q.val($(this).text());
		q.attr("data-lessonid", $(this).attr("data-lessonid"));

		filterLesson($(this).text());
	});
}
function getStuLessonTochoose_desktopHandle(data){
	if(data.totalCount!=undefined) {

   fillDesktopLesson(data.result);
   $("#partpagenavbar_deskmap").pagination({total:data.totalPageCount,
	   handler:function (){
		   var index=$(this).text();
		   getStuLessonsToChoose(index,1,changePage_deskmap);
	   }
	});
    }
    else {
       layer.msg(langThis("kc_queryLessonFail"));
    }

}
function changePage_deskmap(data){
	if(data.totalCount!=undefined) {

   fillDesktopLesson(data.result);}
     else {
       layer.msg(langThis("kc_queryLessonFail"));
    }

}
function getStuLessonTochoose_onlineCodeHandle(data){
	if(data.totalCount!=undefined) {

    fillOnlineCodeLesson(data.result);
    $("#partpagenavbar_onlineCode").pagination({total:data.totalPageCount,
	   handler:function (){
		   var index=$(this).text();
		   getStuLessonsToChoose(index,2,changePage_onlineCode);
	   }
	});
    }
    else {
       layer.msg(langThis("kc_queryLessonFail"));
    }

}
function changePage_onlineCode(){
	if(data.totalCount!=undefined) {

   fillOnlineCodeLesson(data.result);}
     else {
       layer.msg(langThis("kc_queryLessonFail"));
    }
}
function getStuLessonTochoose_onlineMapHandle(data){
	if(data.totalCount!=undefined) {

    fillOnlineMapLesson(data.result);
       $("#partpagenavbar_onlinemap").pagination({total:data.totalPageCount,
	   handler:function (){
		   var index=$(this).text();
		   getStuLessonsToChoose(index,3,changePage_onlineMap);
	   }
	});
    }
    else {
       layer.msg(langThis("kc_queryLessonFail"));
    }

}
function  changePage_onlineMap(){
	if(data.totalCount!=undefined) {

   fillOnlineMapLesson(data.result);}
     else {
       layer.msg(langThis("kc_queryLessonFail"));
    }
}

function  changePage_themeMap(){
	if(data.totalCount!=undefined) {

   fillthemeMapLesson(data.result);}
     else {
       layer.msg(langThis("kc_queryLessonFail"));
    }
}
function getStuLessonTochoose_otherHandle(data){
	if(data.totalCount!=undefined) {
         fillOtherLesson(data.result);
          $("#partpagenavbar_other").pagination({total:data.totalPageCount,
	   handler:function (){
		   var index=$(this).text();
		   getStuLessonsToChoose(index,99,changePage_other);
	   }
	});

    }
    else {
       layer.msg(langThis("kc_queryLessonFail"));
    }

}

function getStuLessonTochoose_themeMapHandle(data){
	if(data.totalCount!=undefined) {
         fillthemeMapLesson(data.result);
          $("#partpagenavbar_thememap").pagination({total:data.totalPageCount,
	   handler:function (){
		   var index=$(this).text();
		   getStuLessonsToChoose(index,4,changePage_themeMap);
	   }
	});

    }
    else {
       layer.msg(langThis("kc_queryLessonFail"));
    }

}
function changePage_other(){
		if(data.totalCount!=undefined) {

   fillOtherLesson(data.result);}
     else {
       layer.msg(langThis("kc_queryLessonFail"));
    }
}
function getStuLessonsToChoose (pageIndex,typeLesson,callback) {
	var param={
                "pagesize":10,
				"pageindex":pageIndex,
				"modeid":2,
				"typeid":typeLesson
    		};
$.ajax({
		url: "publishlessons/queryresult",
		type: "POST",
		contentType:"application/json",
		data:JSON.stringify(param) ,
		dataType: "json",
		success: callback,
		error: function (event) {
           layer.msg(langThis("kc_queryLessonFail"));


		}
	});
}
function  setPagePagination(length){

	if(length>0){
		var n = length% 10;  //每页显示10个
		var totalPage;
		if (n == 0) {
			totalPage = parseInt(length / 10);//丢弃小数部分,保留整数部分
		} else {
			totalPage = parseInt(length / 10) + 1;//丢弃小数部分,保留整数部分
		}
		$('#totalIndex').text(totalPage);
		if (totalPage >= 2) {
			fillTableDetail(0, 9);
		}
		else {
			fillTableDetail(0,length-1);
		}
	}



}
function prePage(){
	var length=Lesson4Choose.length;
	var p=$("#nowIndex");
	var  index=parseInt(p.text());
	if(index!=1){
		p.text(--index);
		var start = index* 10 - 10;
		var stop = index * 10 - 1;
		stop=stop>=length?length-1:stop;
		fillTableDetail(start,stop);
	}
}
function  nextPage(){
	var length=Lesson4Choose.length;
	var p=$("#nowIndex");
	var  index=parseInt(p.text());
	var totalPage=$("#totalIndex").text();
	if(index<totalPage){
		var s=++index;
		p.text(s);
		var start = s* 10 - 10;
		var stop = s * 10 - 1;
		stop=stop>=length?length-1:stop;
		fillTableDetail(start,stop);
	}
}
function  firstPage(){
	var length=Lesson4Choose.length;
	var stop=9;
	stop=	stop>=length?length-1:stop;
	$("#nowIndex").text("1");
	fillTableDetail(0,stop);
}
function  lastPage(){
	var length=Lesson4Choose.length;
	var p=$("#nowIndex");
	var totalPage=$("#totalIndex").text();
	p.text(totalPage);
	var start = totalPage* 10 - 10;
	var stop = totalPage * 10 - 1;
	stop=stop>=length?length-1:stop;
	fillTableDetail(start,stop);
}
function fillTableDetail(start ,stop){
	if(Lesson4Choose.length<1){
		// console.log("Lesson4Choose is null");
		return;
	}
	var  ht="";
    for(var i=start ;i<=stop;i++){
    	var theL=Lesson4Choose[i];
     ht+="<tr><td>"+theL.lesson_id+"</td><td>"+theL.lesson_name+"</td><td>"+theL.description+"</td><td>"+theL.teachername+"</td><td>";
    		if(theL.studenthava){
    		ht+="<span  id='apply_"+theL.lesson_id+"'>"+langThis("ol_passed")+"</span></td></tr>"
    	}else{
    		if(theL.studentget){
    			ht+="<span  id='apply_"+theL.lesson_id+"'>"+langThis("ol_applyed")+"</span></td></tr>"
    		}
    		else{
    			ht+="<a class='blueBtn' onclick='applyLesson(this)' id='apply_"+theL.lesson_id+"'>"+langThis("ol_apply")+"</a></td></tr>"
    		}

    	}

    }
     $("#itemTbody_class").html(ht);
}
function applyLesson(e){
	var p=e.id.split("_");
	var _lessonid=p[1];
	var _phase=p[2];
	var url="publishlessons/"+_lessonid+"/"+_phase+"/studentlist";
	var param= {"userinfoid":UserID};
	$.ajax({
		url:  url,
		type: "POST",
		data:JSON.stringify(param),
		dataType: "json",
		contentType:"application/json",
		success: function (data){
                if(data==true) {
                //success
               layer.msg(langThis("kc_appLessonSuc"));

 				e.disabled=true;
            }
            else {
                layer.msg(langThis("kc_appLessonFail"));
            }

        },
		error: function (event) {
           layer.msg(langThis("kc_appLessonFail"));

		}
	});
}
function filterLesson(lessonidOrName){
	if(Lesson4Choose.length<1){
		// console.log("Lesson4Choose is null");
		return;
	}
	var  filted=[];
	for(var i  in Lesson4Choose){
		var thel=Lesson4Choose[i];
		if(thel.lesson_id==lessonidOrName||thel.lesson_name==lessonidOrName){
			filted.push(thel);
		}
	}
	rewriteTableDetails(filted);
}
function rewriteTableDetails(filted){
	var  ht="";
    for(var i=0 ;i<filted.length;i++){
    	var theL=filted[i];
    ht+="<tr><td>"+theL.lesson_id+"</td><td>"+theL.lesson_name+"</td><td>"+theL.description+"</td><td>"+theL.teachername+"</td><td>";
    	if(theL.studenthava){
    		ht+="<span  id='apply_"+theL.lesson_id+"'>"+langThis("ol_passed")+"</span></td></tr>"
    	}else{
    		if(theL.studentget){
    			ht+="<span  id='apply_"+theL.lesson_id+"'>"+langThis("ol_applyed")+"</span></td></tr>"
    		}
    		else{
    			ht+="<a class='blueBtn' onclick='applyLesson(this)' id='apply_"+theL.lesson_id+"'>"+langThis("ol_apply")+"</a></td></tr>"
    		}

    	}

    }
     $("#itemTbody_class").html(ht);
}
function confirmLessonApply(){
	 layer.confirm(langThis("ol_apply"),{btn:[
        langThis("pm_confirm"),
        langThis("pm_cancle")
    ], title:langThis("lab_atten")},function(){

	  applyLesson();

	  	    layer.closeAll();
    },function(){layer.closeAll();});
}

function fillDesktopLesson(lessons){
var htmlStr='';
for(var i in lessons){
var theLesson=lessons[i];
var paramStr="lessonDetail?i="+theLesson.id+"&p="+theLesson.phase;
htmlStr+="<li class=\"media lessonbottomHr\"><a class=\"media-left\" href=\""+paramStr+"\"><img src=\""+theLesson.image+"\" class=\"lessonimg\"></a><div class=\"media-body\"><h3 class=\"media-heading lessonTitle\">"+theLesson.name+"</h3><p class=\"lessontxt\">"+theLesson.description+"</p><a role=\"button\" class=\"btn lessonBtn\" href=\""+paramStr+"\">"+langThis("ms_lessDetail")+"</a><input type=\"button\" class=\"btn applyBtn\"  value=\""+langThis("ol_apply")+"\"  onclick=\"applyLesson(this)\" id=\"apply_"+theLesson.id+"_"+theLesson.phase+"\" /></div></li>";
}
$("#deskmapUl").html(htmlStr);
}

function fillOnlineCodeLesson(lessons){
var htmlStr='';
for(var i in lessons){
var theLesson=lessons[i];
var paramStr="lessonDetail?i="+theLesson.id+"&p="+theLesson.phase;
htmlStr+="<li class=\"media lessonbottomHr\"><a class=\"media-left\" href=\""+paramStr+"\"><img src=\""+theLesson.image+"\" class=\"lessonimg\"></a><div class=\"media-body\"><h3 class=\"media-heading lessonTitle\">"+theLesson.name+"</h3><p class=\"lessontxt\">"+theLesson.description+"</p><a role=\"button\" class=\"btn lessonBtn\" href=\""+paramStr+"\">"+langThis("ms_lessDetail")+"</a><input type=\"button\" class=\"btn applyBtn\"  value=\""+langThis("ol_apply")+"\"  onclick=\"applyLesson(this)\" id=\"apply_"+theLesson.id+"_"+theLesson.phase+"\" /></div></li>";
}
$("#onlineCodeUl").html(htmlStr);
}
function fillOnlineMapLesson(lessons){
var htmlStr='';
for(var i in lessons){
var theLesson=lessons[i];
var paramStr="lessonDetail?i="+theLesson.id+"&p="+theLesson.phase;
htmlStr+="<li class=\"media lessonbottomHr\"><a class=\"media-left\" href=\""+paramStr+"\"><img src=\""+theLesson.image+"\" class=\"lessonimg\"></a><div class=\"media-body\"><h3 class=\"media-heading lessonTitle\">"+theLesson.name+"</h3><p class=\"lessontxt\">"+theLesson.description+"</p><a role=\"button\" class=\"btn lessonBtn\" href=\""+paramStr+"\">"+langThis("ms_lessDetail")+"</a><input type=\"button\" class=\"btn applyBtn\"  value=\""+langThis("ol_apply")+"\"  onclick=\"applyLesson(this)\" id=\"apply_"+theLesson.id+"_"+theLesson.phase+"\" /></div></li>";
}
$("#onlinemapUl").html(htmlStr);
}
function fillthemeMapLesson(lessons){
var htmlStr='';
for(var i in lessons){
var theLesson=lessons[i];
var paramStr="lessonDetail?i="+theLesson.id+"&p="+theLesson.phase;
htmlStr+="<li class=\"media lessonbottomHr\"><a class=\"media-left\" href=\""+paramStr+"\"><img src=\""+theLesson.image+"\" class=\"lessonimg\"></a><div class=\"media-body\"><h3 class=\"media-heading lessonTitle\">"+theLesson.name+"</h3><p class=\"lessontxt\">"+theLesson.description+"</p><a role=\"button\" class=\"btn lessonBtn\" href=\""+paramStr+"\">"+langThis("ms_lessDetail")+"</a><input type=\"button\" class=\"btn applyBtn\"  value=\""+langThis("ol_apply")+"\"  onclick=\"applyLesson(this)\" id=\"apply_"+theLesson.id+"_"+theLesson.phase+"\" /></div></li>";
}
$("#thememapUl").html(htmlStr);
}
function fillOtherLesson(lessons){
var htmlStr='';
for(var i in lessons){
var theLesson=lessons[i];
var paramStr="lessonDetail?i="+theLesson.id+"&p="+theLesson.phase;
htmlStr+="<li class=\"media lessonbottomHr\"><a class=\"media-left\" href=\""+paramStr+"\"><img src=\""+theLesson.image+"\" class=\"lessonimg\"></a><div class=\"media-body\"><h3 class=\"media-heading lessonTitle\">"+theLesson.name+"</h3><p class=\"lessontxt\">"+theLesson.description+"</p><a role=\"button\" class=\"btn lessonBtn\" href=\""+paramStr+"\">"+langThis("ms_lessDetail")+"</a><input type=\"button\" class=\"btn applyBtn\"  value=\""+langThis("ol_apply")+"\"  onclick=\"applyLesson(this)\" id=\"apply_"+theLesson.id+"_"+theLesson.phase+"\" /></div></li>";
}
$("#otherUl").html(htmlStr);
}